package com.sharejoys.propertyanimationdemo;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;

/**
 * Created by xiSky on 2018/2/4
 *
 * @since 1.0
 */

public class SafariAnimationUI extends AppCompatActivity {
    private Button bt;
    private View first_View;
    private View second_View;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
//        requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题栏
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_safari_animation);
        bt = findViewById(R.id.bt);
        first_View = findViewById(R.id.first);
        second_View = findViewById(R.id.second);
    }

    public void startFirstAnimation(View view) {
        //first_View动画：1.翻转动画；2.透明度动画；3.缩放动画
        //翻转
        ObjectAnimator firstRotationAnim = ObjectAnimator.ofFloat(first_View, "rotationX", 0, 25);
        firstRotationAnim.setDuration(300);

        //透明度
        ObjectAnimator firstAlphaAnim = ObjectAnimator.ofFloat(first_View, "alpha", 1f, 0.5f);
        firstAlphaAnim.setDuration(200);

        //缩放动画
        ObjectAnimator firstViewScaleXAnim = ObjectAnimator.ofFloat(first_View, "scaleX", 1f, 0.8f);
        firstViewScaleXAnim.setDuration(300);
        ObjectAnimator firstViewScaleYAnim = ObjectAnimator.ofFloat(first_View, "scaleY", 1f, 0.8f);
        firstViewScaleYAnim.setDuration(300);

        //改正向旋转设置监听，执行完毕后再执行反向旋转
        ObjectAnimator firstRusumeRotationXAnim = ObjectAnimator.ofFloat(first_View, "rotationX", 25f, 0);
        firstRusumeRotationXAnim.setDuration(200);
        firstRusumeRotationXAnim.setStartDelay(200);//延迟执行

        //由于缩放造成了离顶部有一段距离，需要平移上去
        ObjectAnimator firstTranslationAnim = ObjectAnimator.ofFloat(first_View, "translationY", 0f, -0.1f * first_View.getHeight());
        firstTranslationAnim.setDuration(200);

        //第二个View执行平移动画--网上平移
        final ObjectAnimator secondTranslationYAnim = ObjectAnimator.ofFloat(first_View, "translationY", second_View.getHeight(), 0);
        secondTranslationYAnim.setDuration(200);
        secondTranslationYAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                second_View.setVisibility(View.VISIBLE);
                bt.setClickable(false);
            }
        });

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.playTogether(
                firstRotationAnim,
                firstAlphaAnim,
                firstViewScaleXAnim,
                firstViewScaleYAnim,
                firstRusumeRotationXAnim,
                firstTranslationAnim,
                secondTranslationYAnim);
        animatorSet.start();

    }

    public void startSecondAnimation(View view) {
//        //first_View动画：1.翻转动画；2.透明度动画；3.缩放动画
//        //翻转
//        ObjectAnimator firstRotationAnim = ObjectAnimator.ofFloat(first_View, "rotationX", 0, 25);
//        firstRotationAnim.setDuration(300);
//
//        //透明度
//        ObjectAnimator firstAlphaAnim = ObjectAnimator.ofFloat(first_View, "alpha", 0.5f, 1f);
//        firstAlphaAnim.setDuration(200);
//
//        //缩放动画
//        ObjectAnimator firstViewScaleXAnim = ObjectAnimator.ofFloat(first_View, "scaleX", 0.8f, 1f);
//        firstViewScaleXAnim.setDuration(300);
//        ObjectAnimator firstViewScaleYAnim = ObjectAnimator.ofFloat(first_View, "scaleY", 0.8f, 1f);
//        firstViewScaleYAnim.setDuration(300);
//
//        //改正向旋转设置监听，执行完毕后再执行反向旋转
//        ObjectAnimator firstRusumeRotationXAnim = ObjectAnimator.ofFloat(first_View, "rotationX", 25f,0);
//        firstRusumeRotationXAnim.setDuration(200);
//        firstRusumeRotationXAnim.setStartDelay(200);//延迟执行
//
//        //由于缩放造成了离顶部有一段距离，需要平移上去
//        ObjectAnimator firstTranslationAnim = ObjectAnimator.ofFloat(first_View, "translationY", -0.1f * first_View.getHeight(), 0f);
//        firstTranslationAnim.setDuration(200);
//
//        //第二个View执行平移动画--网下平移
//        final ObjectAnimator secondTranslationYAnim = ObjectAnimator.ofFloat(first_View, "translationY", 0, second_View.getHeight());
//        secondTranslationYAnim.setDuration(200);
//        secondTranslationYAnim.addListener(new AnimatorListenerAdapter() {
//            @Override
//            public void onAnimationStart(Animator animation) {
//                super.onAnimationStart(animation);
//                second_View.setVisibility(View.VISIBLE);
//            }
//
//            @Override
//            public void onAnimationEnd(Animator animation) {
//                super.onAnimationEnd(animation);
//                bt.setEnabled(true);
//            }
//        });
//
//        AnimatorSet animatorSet = new AnimatorSet();
//        animatorSet.playTogether(firstRotationAnim,
//                firstAlphaAnim,
//                firstViewScaleXAnim,
//                firstViewScaleYAnim,
//                firstRusumeRotationXAnim,
//                firstTranslationAnim,
//                secondTranslationYAnim);
//        animatorSet.start();
        /**
         * 1）first_View动画：1.翻转动画；2.透明度动画；3.缩放动画
         */
        //1.翻转
        ObjectAnimator firstRotationAnim = ObjectAnimator.ofFloat(first_View, "rotationX", 0f, 25f);
        firstRotationAnim.setDuration(300);
//		firstRotationAnim.start();
        //2.透明度
        ObjectAnimator firstAlphaAnim = ObjectAnimator.ofFloat(first_View, "alpha", 0.5f, 1f);
        firstAlphaAnim.setDuration(200);
        //3.缩放动画
        ObjectAnimator firstScaleXAnim = ObjectAnimator.ofFloat(first_View, "scaleX", 0.8f, 1f);
        firstScaleXAnim.setDuration(300);
        ObjectAnimator firstScaleYAnim = ObjectAnimator.ofFloat(first_View, "scaleY", 0.8f, 1f);
        firstScaleYAnim.setDuration(300);
        //改正向旋转设置监听，执行完毕后再执行反向旋转
//		firstRotationAnim.addUpdateListener(listener)
        ObjectAnimator firstResumeRotationAnim = ObjectAnimator.ofFloat(first_View, "rotationX", 25f, 0f);
        firstResumeRotationAnim.setDuration(200);
        firstResumeRotationAnim.setStartDelay(200);//延迟执行
        //由于缩放造成了离顶部有一段距离，需要平移上去
        ObjectAnimator firstTranslationAnim = ObjectAnimator.ofFloat(first_View, "translationY", -0.1f * first_View.getHeight(), 0f);
        firstTranslationAnim.setDuration(200);
        //第二个View执行平移动画--网上平移
        ObjectAnimator secondeTranslationAnim = ObjectAnimator.ofFloat(second_View, "translationY", 0f, second_View.getHeight());
        secondeTranslationAnim.setDuration(300);
        secondeTranslationAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                second_View.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                bt.setClickable(true);
            }

        });

        AnimatorSet set = new AnimatorSet();
        set.playTogether(firstRotationAnim, firstAlphaAnim, firstScaleXAnim, firstScaleYAnim, firstResumeRotationAnim, firstTranslationAnim, secondeTranslationAnim);
        set.start();
    }
}
